using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Coding4Fun.Phone.Controls.Toolkit.Common
{
	public class TimeSpanFormat
	{
		public static string Format(TimeSpan time, string format)
		{
			StringBuilder stringBuilder = new StringBuilder(format);
			MatchCollection matchCollection = Regex.Matches(format, "{0:[^}]*}");
			List<Match> list = Enumerable.ToList<Match>(Enumerable.Cast<Match>(matchCollection));
			list.Reverse();
			using (List<Match>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Match current = enumerator.get_Current();
					StringBuilder stringBuilder2 = new StringBuilder(current.get_Value());
					MatchCollection matchCollection2 = Regex.Matches(current.get_Value(), "[d]{1,2}|[h]{1,2}|[m]{1,2}|[s]{1,2}|[f]{1,7}|[F]{1,7}");
					List<Match> list2 = Enumerable.ToList<Match>(Enumerable.Cast<Match>(matchCollection2));
					list2.Reverse();
					using (List<Match>.Enumerator enumerator2 = list2.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Match current2 = enumerator2.get_Current();
							char c = current2.get_Value().get_Chars(0);
							if (c <= 'h')
							{
								if (c != 'F')
								{
									switch (c)
									{
									case 'd':
										stringBuilder2.Replace(current2.get_Value(), time.get_Days().ToString("00"), current2.get_Index(), current2.get_Length());
										break;
									case 'f':
										stringBuilder2.Replace(current2.get_Value(), (time.get_Milliseconds() / 1000).ToString(current2.get_Value().Replace("f", "0")), current2.get_Index(), current2.get_Length());
										break;
									case 'h':
										stringBuilder2.Replace(current2.get_Value(), time.get_Hours().ToString("00"), current2.get_Index(), current2.get_Length());
										break;
									}
								}
								else
								{
									stringBuilder2.Replace(current2.get_Value(), ((double)time.get_Milliseconds() / 1000.0).ToString(current2.get_Value().Replace("F", "0")), current2.get_Index(), current2.get_Length());
								}
							}
							else
							{
								if (c != 'm')
								{
									if (c == 's')
									{
										stringBuilder2.Replace(current2.get_Value(), time.get_Seconds().ToString("00"), current2.get_Index(), current2.get_Length());
									}
								}
								else
								{
									stringBuilder2.Replace(current2.get_Value(), time.get_Minutes().ToString("00"), current2.get_Index(), current2.get_Length());
								}
							}
						}
					}
					stringBuilder2.Remove(0, 3);
					stringBuilder2.Remove(stringBuilder2.get_Length() - 1, 1);
					stringBuilder.Replace(current.get_Value(), stringBuilder2.ToString(), current.get_Index(), current.get_Length());
				}
			}
			return stringBuilder.ToString();
		}
	}
}
